package OssClinet

import (
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

type Client struct {
	raw *oss.Client
}

func NewClient(cfg Config) (*Client, error) {
	c, err := oss.New(cfg.Endpoint, cfg.AccessKeyID, cfg.AccessKeySecret)
	if err != nil {
		return nil, fmt.Errorf("failed to create oss client: %w", err)
	}
	return &Client{raw: c}, nil
}

func (c *Client) ListBuckets() ([]oss.BucketProperties, error) {
	res, err := c.raw.ListBuckets()
	if err != nil {
		return nil, err
	}
	return res.Buckets, nil
}

func (c *Client) CreateBucket(name string) error {
	return c.raw.CreateBucket(name)
}

func (c *Client) DeleteBucket(name string) error {
	return c.raw.DeleteBucket(name)
}

func (c *Client) Bucket(name string) (*Bucket, error) {
	b, err := c.raw.Bucket(name)
	if err != nil {
		return nil, err
	}
	return &Bucket{raw: b}, nil
}
